package Server.Dao;

import Server.Bean.Express;
import Server.User;

import java.io.*;
import java.util.*;

public class ExpressDao implements Serializable{

    private  HashMap<String, Express> data = new HashMap(100,0.75f);
    private   HashMap<String, User> user = new HashMap(100,0.75f);
    private  HashMap<String, User> admin = new HashMap(100,0.75f);

    private Collection<Express> values = data.values();
    private  Collection<User> userValues = user.values();
    private   Collection<User> adminValues = admin.values();

    private  Set<String> set = data.keySet();
    int size=0;

    private static final String EXPRESS_FILE_NAME= "express2.txt";
    private static final String ADMIN_FILE_NAME= "admin.txt";
    private static final String USER_FILE_NAME= "user.txt";

    private static final File userFile = new File(USER_FILE_NAME);
    private static final File adminFile = new File(ADMIN_FILE_NAME);
    private static final File expressFile = new File(EXPRESS_FILE_NAME);

    Random random = new Random();
    static{
        //需要的某个文件不存在则创建文件
        if(!userFile.exists() || adminFile.exists() || expressFile.exists()){
            try {
                adminFile.createNewFile();
                userFile.createNewFile();
                expressFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public ExpressDao()  {
        try {
            FileOutputStream fos = new FileOutputStream(userFile);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            User user1 = new User();
            user1.setUsername("user");
            user1.setPassword("123");
            user.put(user1.getUsername(),user1);
            oos.writeObject(user);
            oos.flush();
            oos = new ObjectOutputStream(new FileOutputStream(adminFile));
            User user2 = new User();
            user2.setUsername("admin");
            user2.setPassword("abc");
            admin.put(user2.getUsername(),user2);
            oos.writeObject(admin);
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            expressRead();
            userRead();
            adminRead();
            values = data.values();
            userValues = user.values();
            adminValues = admin.values();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 存储快递
     * @param e
     * @return
     */
    public Express add(Express e) throws Exception {
        if(size==100){
            throw new Exception("快递柜已满！");
        }
        int x,y;
        do {
            //1.随机生成两个0-9的数据x和y
            x=random.nextInt(10);
            y=random.nextInt(10);
        }while (isExist(x,y));//生成快递的位置
        e.setX(x);
        e.setY(y);
        int code = randomcode();//生成取件码
        e.setCode(code);
        size++;//快递数量+1
        Express e1 = e;
        if(data.put(e.getNumber(),e)==null){//快递不存在data.put()返回null 存在则返回此对象
            return e;//存储成功将添加了取件码和位置的快递对象返回
        }
        return null;
    }

    /**
     * 随机生成100000-999999的随机数
     * @return
     */
    private int randomcode(){
        //生成100000-999999的随机数
        while (true){
            int code = random.nextInt(900000)+100000;
            Express e = findBycode(code);
            if(e==null){
                return code;
            }
        }

    }

    /**
     * 根据快递单号查询快递
     * @param number
     * @return 快递对象
     */
    public Express findBynumber(String number){
        Express e = data.get(number);//不存在返回null
        return e==null?null:e;

    }

    /**
     * 根据取件码查询快递
     * @param code
     * @return 查询的结果，失败返回null;
     */
    public Express findBycode(int code){
        for (Express express : values) {
            if(express.getCode()==code){
                return express;
            }

        }
        return null;
    }

    /**
     * 为了符合MVC做的多余的操作
     * @param
     * @param newExpress
     */
    public Express update(String Number,Express newExpress) throws Exception {
        delete(Number);
        Express e = add(newExpress);
        return e;
    }

    /**
     * 删除快递
     * @param Number
     */
    public void delete(String Number){
        data.remove(Number);
    }
    public HashMap<String, Express> findall(){
        return data;
    }

    /**
     * 判断快递柜的这个位置是否有快递
     * @param x
     * @param y
     * @return
     */
    public boolean isExist(int x,int y){
        for (Express express:values) {
            if(express.getX()==x && express.getY()==y){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否有这个用户
     * @param u
     * @return 查找结果
     */
    public boolean userFindByNameAndByPwd(User u) {
        for (User u1 : userValues) {
            if (u.getUsername().equals(u1.getUsername()) && u.getPassword().equals(u1.getPassword())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否有这个管理员
     * @param u
     * @return 查找结果
     */
    public boolean adminFindByNameAndByPwd(User u) {
        for (User u1 : adminValues) {
            if (u.getUsername().equals(u1.getUsername()) && u.getPassword().equals(u1.getPassword())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 读取存储User对象的文件
     * 将读取的内容传给 user -- HashMap
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void userRead() throws IOException, ClassNotFoundException {
        if (EXPRESS_FILE_NAME.length() > 0) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(this.userFile));
            user = (HashMap<String,User>) ois.readObject();
            ois.close();
        }
    }

    /**
     * 将user对象写入文件USER_FINE_NAME
     * @throws IOException
     */
    public void userWrite() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.userFile));
        oos.writeObject(this.user);
        oos.close();
    }

    /**
     * 读取存储管理员对象的文件
     * 将读取的内容传给 admin -- HashMap
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void adminRead() throws IOException, ClassNotFoundException {
        if (EXPRESS_FILE_NAME.length() > 0) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(this.adminFile));
            admin = (HashMap<String,User>) ois.readObject();
            ois.close();
        }
    }

    /**
     * 将管理员对象写入文件ADMIN_FINE_NAME
     * @throws IOException
     */
    public void adminWrite() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.adminFile));
        oos.writeObject(this.admin);
        oos.close();
    }

    /**
     * 读取存储快递对象的文件
     * 将读取的内容传给 data -- HashMap
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void expressRead() throws IOException, ClassNotFoundException {
        if (expressFile.length()>0) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(expressFile));
            data = (HashMap<String,Express>) ois.readObject();
            ois.close();
        }
    }

    /**
     *将存储快递对象的data写入文件EXPRESS_FILE_NAME
     * @throws IOException
     */
    public void expressWrite() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(expressFile));
        oos.writeObject(this.data);
        oos.close();
    }
}

